/*
 * Created on 25. aug.. 2006
 * Created by Tomas Ekeli
 */
package people;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


/**
 * 
 * A group of Persons. The group of persons has a name and a score, which is
 * used to manipulate the total perceived score of the persons with 
 * membership in the group.
 * 
 * @author Tomas Ekeli
 *
 */
public class Group implements Reserver{
	private String name;
	private int score;
	private LinkedList<Person> members;
	
	public String getName() {
		return name;
	}

	public Group(String n) {
		name = n;
		score = 0;
		members = new LinkedList<Person>();
	}
	
	public Group(String n, int s) {
		name = n;
		score = s;
		members = new LinkedList<Person>();
	}
	
	protected void setName(String newName) {
		name = newName;
	}
	
	public int getScore() {
		return score;
	}
	
	public void setScore(int n) {
		score = n;
	}
	
	/**
	 * Adds a Person to the Group, adding the score of the Group to the Person's.
	 * A Person cannot hold more than one membership in a group.
	 * @param p The Person to add.
	 */
	public void addPerson(Person p) {
		if (!inGroup(p)) {
			members.add(p);
			p.addToScore(score);
		}
	}
	
	/**
	 * Removes the Person specified from the Group, decrementing the score
	 * of that person by this Group's score.
	 * @param p The Person to remove
	 * @throws PersonNotFoundException iff the Person was not a member.
	 */
	public void removePerson(Person p) throws PersonNotFoundException {
		if (inGroup(p)) {
			members.remove(p);
			p.addToScore(0-score);
		}
		else {
			throw new PersonNotFoundException(p.getID() +" not found in group.");
		}
	}
	
	/**
	 * @return A List of all the Persons currently in the group.
	 */
	public List<Person> getPersons() {
		return new LinkedList<Person>(members);
	}
	
	/**
	 * Check whether the Person is a member of the Group
	 * @param p The person to check
	 * @return true iff the Person is in the Group
	 */
	public boolean inGroup(Person p) {
		boolean found = false;
		for (Iterator<Person> iter = members.iterator(); iter.hasNext() && found == false;) {
			found = iter.next().equals(p);
		}
		return found;
	}

}
